package com.cskaoyan.service;

import com.cskaoyan.bean.CskaoyanmallComment;
import com.cskaoyan.bean.CskaoyanmallCommentExample;
import com.cskaoyan.mapper.CskaoyanmallCommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.vo.CommentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CskaoyanmallCommentServiceImpl implements CskaoyanmallCommentService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private CskaoyanmallCommentMapper cskaoyanmallCommentMapper;

    @Override
    public long countByExample(CskaoyanmallCommentExample example) {
        return cskaoyanmallCommentMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(CskaoyanmallCommentExample example) {
        return cskaoyanmallCommentMapper.deleteByExample(example);
    }

    @Override
    public List<CskaoyanmallComment> selectByExample(CskaoyanmallCommentExample example) {
        return cskaoyanmallCommentMapper.selectByExample(example);
    }

    @Override
    public CskaoyanmallComment selectByPrimaryKey(Integer id) {
        return cskaoyanmallCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(CskaoyanmallComment record, CskaoyanmallCommentExample example) {
        return cskaoyanmallCommentMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(CskaoyanmallComment record, CskaoyanmallCommentExample example) {
        return cskaoyanmallCommentMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(CskaoyanmallComment record) {
        return cskaoyanmallCommentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CskaoyanmallComment record) {
        return cskaoyanmallCommentMapper.updateByPrimaryKey(record);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public CommentVo query(Integer page, Integer limit, String sort, String order) {
        //分页和排序
        //页码、数据量
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset

        CskaoyanmallCommentExample commentExample = new CskaoyanmallCommentExample();
        commentExample.setOrderByClause(sort + " " + order);//${}做Sql拼接
        CskaoyanmallCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<CskaoyanmallComment> comments = cskaoyanmallCommentMapper.selectByExample(commentExample);
        System.out.println(comments.get(1));
        //total !=comment.size();
        //total是满足条件的所有的数据量 👉 而不是当前的limit
        //可以通过PageInfo获得分页信息
        PageInfo<CskaoyanmallComment> userPageInfo = new PageInfo<>(comments);//查询结果放进来
        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample

        CommentVo commentVo = new CommentVo();
        commentVo.setItems(comments);
        commentVo.setTotal(total);
        return commentVo;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public int delete(CskaoyanmallComment cskaoyanmallComment) {
        cskaoyanmallComment.setDeleted(true);
        int i = updateByPrimaryKey(cskaoyanmallComment);
        if (i == 1) {
            return 0;
        }else {
            return 400;
        }
    }


    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public CommentVo select(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);

        List<CskaoyanmallComment> comments = cskaoyanmallCommentMapper.selectAllByUserIdOrValueId(userId, valueId);
        PageInfo<CskaoyanmallComment> userPageInfo = new PageInfo<>(comments);//查询结果放进来
        long total = userPageInfo.getTotal();//总的数据量 👉 select count(*) 条件是byExample
        CommentVo commentVo = new CommentVo();
        commentVo.setItems(comments);
        commentVo.setTotal(total);
        return commentVo;
    }
}
